package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.IDao;
import com.mohe.dangjian.model.CommunityParty;
import com.mohe.dangjian.service.interfaces.ICommunityParty;
import com.mohe.dangjian.utils.Page;

public class CommunityPartyImpl implements ICommunityParty {

	private IDao<CommunityParty> communityPartyDao;

	public IDao<CommunityParty> getCommunityPartyDao() {
		return communityPartyDao;
	}

	public void setCommunityPartyDao(IDao<CommunityParty> communityPartyDao) {
		this.communityPartyDao = communityPartyDao;
	}

	@Override
	public List<CommunityParty> getCommunityPartyListForPage(
			CommunityParty communityParty, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 党组织名称
		if (!StringUtils.isBlank(communityParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(communityParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + communityParty.getAddress() + "%%");
		}
		// 居委会电话
		if (!StringUtils.isBlank(communityParty.getTel())) {
			where.append(" and tel like ?");
			params.add("%%" + communityParty.getTel() + "%%");
		}
		// 党支部书记
		if (!StringUtils.isBlank(communityParty.getSecretary())) {
			where.append(" and secretary like ?");
			params.add("%%" + communityParty.getSecretary() + "%%");
		}
		// 党支部书记联系方式
		if (!StringUtils.isBlank(communityParty.getTel1())) {
			where.append(" and tel1 like ?");
			params.add("%%" + communityParty.getTel1() + "%%");
		}
		// 居委会主任
		if (!StringUtils.isBlank(communityParty.getDirector())) {
			where.append(" and director like ?");
			params.add("%%" + communityParty.getDirector() + "%%");
		}
		// 居委会主任联系方式
		if (!StringUtils.isBlank(communityParty.getTel2())) {
			where.append(" and tel2 like ?");
			params.add("%%" + communityParty.getTel2() + "%%");
		}

		where.append(" order by createDate desc");
		List<CommunityParty> list = communityPartyDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(CommunityParty communityParty) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// TODO 查询条件待补充
		// 党组织名称
		if (!StringUtils.isBlank(communityParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(communityParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + communityParty.getAddress() + "%%");
		}
		// 居委会电话
		if (!StringUtils.isBlank(communityParty.getTel())) {
			where.append(" and tel like ?");
			params.add("%%" + communityParty.getTel() + "%%");
		}
		// 党支部书记
		if (!StringUtils.isBlank(communityParty.getSecretary())) {
			where.append(" and secretary like ?");
			params.add("%%" + communityParty.getSecretary() + "%%");
		}
		// 党支部书记联系方式
		if (!StringUtils.isBlank(communityParty.getTel1())) {
			where.append(" and tel1 like ?");
			params.add("%%" + communityParty.getTel1() + "%%");
		}
		// 居委会主任
		if (!StringUtils.isBlank(communityParty.getDirector())) {
			where.append(" and director like ?");
			params.add("%%" + communityParty.getDirector() + "%%");
		}
		// 居委会主任联系方式
		if (!StringUtils.isBlank(communityParty.getTel2())) {
			where.append(" and tel2 like ?");
			params.add("%%" + communityParty.getTel2() + "%%");
		}

		return communityPartyDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveCommunityParty(CommunityParty communityParty) {
		return communityPartyDao.insert(communityParty);
	}

	@Override
	public boolean saveCommunityPartyList(
			List<CommunityParty> communityPartyList) {
		return communityPartyDao.insert(communityPartyList);
	}

	@Override
	public CommunityParty getCommunityPartyById(Long timeKey) {
		CommunityParty communityParty = communityPartyDao.getByKey(timeKey);
		if (communityParty != null) {
			return communityParty;
		}
		return null;
	}

	@Override
	public boolean updateCommunityParty(CommunityParty communityParty) {
		return communityPartyDao.update(communityParty);
	}

	@Override
	public boolean delCommunityParty(CommunityParty communityParty) {
		return communityPartyDao.delete(communityParty);
	}

	@Override
	public List<CommunityParty> getAllCommunityParty() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return communityPartyDao.query(where.toString(), null);
	}

	@Override
	public List<CommunityParty> getCommunityPartyList(
			CommunityParty communityParty) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 党组织名称
		if (!StringUtils.isBlank(communityParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(communityParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + communityParty.getAddress() + "%%");
		}
		// 居委会电话
		if (!StringUtils.isBlank(communityParty.getTel())) {
			where.append(" and tel like ?");
			params.add("%%" + communityParty.getTel() + "%%");
		}
		// 党支部书记
		if (!StringUtils.isBlank(communityParty.getSecretary())) {
			where.append(" and secretary like ?");
			params.add("%%" + communityParty.getSecretary() + "%%");
		}
		// 党支部书记联系方式
		if (!StringUtils.isBlank(communityParty.getTel1())) {
			where.append(" and tel1 like ?");
			params.add("%%" + communityParty.getTel1() + "%%");
		}
		// 居委会主任
		if (!StringUtils.isBlank(communityParty.getDirector())) {
			where.append(" and director like ?");
			params.add("%%" + communityParty.getDirector() + "%%");
		}
		// 居委会主任联系方式
		if (!StringUtils.isBlank(communityParty.getTel2())) {
			where.append(" and tel2 like ?");
			params.add("%%" + communityParty.getTel2() + "%%");
		}

		where.append(" order by createDate desc");
		List<CommunityParty> list = communityPartyDao.query(where.toString(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}
}
